﻿ using Maticsoft.BLL;
    using Maticsoft.DBUtility;
    using Maticsoft.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Utils;
    using Wuqi.Webdiyer;

    public partial class WebPage_Dingdan_Service : OrderBBasePage
    {

        public Maticsoft.BLL.Coupon coupbll = new Maticsoft.BLL.Coupon();
        public Maticsoft.Model.Coupon coupmodel = new Maticsoft.Model.Coupon();
        public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
        public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();

        public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
        public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
        public int pagenum = 0;
        protected string service;
        protected string serviceValue;
        private string sqlwhere = " 1=1  ";
        public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
        public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
        public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
        public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
        private string time;
        public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
        public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.pagenum = 0;
            this.getWeifu();
        }

        private void deleteOrder(int id)
        {
            new Maticsoft.BLL.Order().Delete(id);
        }

        public string getTime()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=''></option>");
            for (int i = 0; i < 0x18; i++)
            {
                builder.Append(string.Concat(new object[] { "<option value='", i, "'>", i, "</option>" }));
            }
            return builder.ToString();
        }

        private void getType()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<tr >");
            builder.Append("<th width='40'>ID</th>");
            builder.Append("<th class='xiangmu'>项目</th>");
            builder.Append("<th width='70'>用户</th>");
            builder.Append("<th width='30' nowrap>数量</th>");
            builder.Append("<th width='50' nowrap>总款</th>");
            builder.Append("<th width='50' nowrap>支付方式</th>");
            builder.Append("<th width='50' nowrap>余付</th>");
            builder.Append("<th width='50' nowrap>支付</th>");
            builder.Append("<th width='30' nowrap>操作</th>");
            builder.Append("</tr>");
            this.Literal1.Text = builder.ToString();
        }

        private void getWeifu()
        {
            int num;
            this.sqlwhere = " service='" + this.service + "' ";
            if (this.uname.Value != "")
            {
                this.sqlwhere = this.sqlwhere + " and username like '" + this.uname.Value + "%'";
            }
            if (this.uemail.Value != "")
            {
                this.sqlwhere = this.sqlwhere + " and Email like '" + this.uemail.Value + "%'";
            }
            if (this.team_id.Value != "")
            {
                this.sqlwhere = this.sqlwhere + " and Team_id=" + this.team_id.Value;
            }
            string where = this.sqlwhere + " and (pay_time between '" + this.time + " 00:00:00' and '" + this.time + " 23:59:59')";
            StringBuilder builder = new StringBuilder();
            Maticsoft.Model.Team model = new Maticsoft.Model.Team();
            Maticsoft.BLL.Team team2 = new Maticsoft.BLL.Team();
            Maticsoft.Model.UserInfo info = new Maticsoft.Model.UserInfo();
            Maticsoft.BLL.UserInfo info2 = new Maticsoft.BLL.UserInfo();
            Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
            Maticsoft.Model.Order order2 = new Maticsoft.Model.Order();
            if (this.pagenum == 0)
            {
                this.pagenum = this.AspNetPager1.CurrentPageIndex;
            }
            string tableName = "(select [order].*,[user].email,Username from [order] inner join [user] on([order].User_id=[user].id)) ttt1";
            DataSet set = DbHelperSQL.Page(this.AspNetPager1.PageSize, this.pagenum, tableName, where, " pay_time desc, Id desc", "*", out num);
            if ((set.Tables.Count > 0) && (set.Tables[0].Rows.Count > 0))
            {
                int num2 = 0;
                int num3 = 0;
                for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                {
                    DataRow row = set.Tables[0].Rows[i];
                    if (Convert.ToInt32(row["Id"].ToString()) != num2)
                    {
                        if ((num3 % 2) == 0)
                        {
                            builder.Append("<tr>");
                        }
                        else
                        {
                            builder.Append("<tr class='alt'>");
                        }
                        builder.Append("<td>" + row["Id"].ToString() + "</td>");
                        num2 = Convert.ToInt32(row["Id"].ToString());
                        model = team2.GetModel(int.Parse(row["Team_id"].ToString()));
                        if (model == null)
                        {
                            this.detaillist = this.detailbll.Searchorderdetail(Convert.ToInt32(row["Id"].ToString()));
                            builder.Append("<td>&nbsp;");
                            int num5 = 0;
                            foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                            {
                                num5++;
                                builder.Append(string.Concat(new object[] { "(<a class='deal-title' href='../team.aspx?id=", orderdetail.Teamid, "' target='_blank'>", num5, ":" }));
                                builder.Append((this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)) == null) ? "" : this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)).Title);
                                builder.Append("</a>)<br>");
                            }
                            builder.Append("</td>");
                        }
                        else
                        {
                            builder.Append(string.Concat(new object[] { "<td>", row["Team_id"], "&nbsp;(<a class='deal-title' href='/team.aspx?id=", row["Team_id"].ToString(), "' target='_blank'>", model.Title, "</a>)</td>" }));
                        }
                        info = info2.GetModel(int.Parse(row["User_id"].ToString()));
                        if (info != null)
                        {
                            builder.Append(string.Concat(new object[] { "<td><a href='/ajax/manage.aspx?action=userview&id=", row["User_id"], "' class='ajaxlink'>", info.Email, "<br/>", info.Username, "</a>&nbsp;&raquo;&nbsp;<a href='/ajax/misc.aspx?action=sms&v=", info.Mobile, "' class='ajaxlink'>短信</a></td>" }));
                        }
                        else
                        {
                            builder.Append("<td>&nbsp;</td>");
                        }
                        builder.Append("<td>" + row["Quantity"] + "</td>");
                        string currency = "\x00a5";
                        if (base.ASSystem != null)
                        {
                            currency = base.ASSystem.Currency;
                        }
                        builder.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", row["Origin"], "</td>" }));
                        if (row["Service"].ToString().ToLower() == "yeepay")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>易宝</td>");
                        }
                        else if (row["Service"].ToString().ToLower() == "alipay")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>支付宝</td>");
                        }
                        else if (row["Service"].ToString().ToLower() == "tenpay")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>财富通</td>");
                        }
                        else if (row["Service"].ToString().ToLower() == "chinamobilepay")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>移动支付</td>");
                        }
                        else if (row["Service"].ToString().ToLower() == "chinabank")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>网银在线</td>");
                        }
                        else if (row["Service"].ToString().ToLower() == "credit")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>余额付款</td>");
                        }
                        else if (row["Service"].ToString().ToLower() == "cash")
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>线下支付</td>");
                        }
                        else
                        {
                            builder.Append("<td><span class='money'>&nbsp;</span>未支付</td>");
                        }
                        builder.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", row["Credit"], "</td>" }));
                        builder.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", row["Money"], "</td>" }));
                        builder.Append("<td class='op' nowrap><a class='ajaxlink' href='/ajax/manage.aspx?action=orderview&orderview=" + row["id"] + "&pagenum=1'>详情</a></td>");
                        builder.Append("</tr>");
                        num3++;
                    }
                }
            }
            this.AspNetPager1.RecordCount = num;
            this.Literal2.Text = builder.ToString();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.HeaderControl1.Select = "order";
            if ((((base.Request.QueryString["service"] != null) && (base.Request.QueryString["time"] != null)) && (base.Request.QueryString["service"].ToString() != "")) && (base.Request.QueryString["time"].ToString() != ""))
            {
                switch (base.Request.QueryString["service"].ToString().ToLower())
                {
                    case "yeepay":
                        this.serviceValue = "易宝付款";
                        break;

                    case "alipay":
                        this.serviceValue = "支付宝付款";
                        break;

                    case "tenpay":
                        this.serviceValue = "财富通付款";
                        break;

                    case "chinabank":
                        this.serviceValue = "网银在线";
                        break;

                    case "credit":
                        this.serviceValue = "余额付款";
                        break;

                    case "cash":
                        this.serviceValue = "线下支付";
                        break;

                    case "chinamobilepay":
                        this.serviceValue = "中国移动付款";
                        break;
                }
                this.Session.Add("service", base.Request.QueryString["service"].ToString());
                this.Session.Add("time", base.Request.QueryString["time"].ToString());
                this.Session.Add("serviceValue", this.serviceValue);
            }
            if (((this.Session["service"] != null) && (this.Session["time"] != null)) && (this.Session["serviceValue"] != null))
            {
                this.service = this.Session["service"].ToString();
                this.time = this.Session["time"].ToString();
                this.serviceValue = this.Session["serviceValue"].ToString();
            }
            if (base.Request["pagenum"] != null)
            {
                if (Utility.IsNum(base.Request["pagenum"].ToString()))
                {
                    this.pagenum = Convert.ToInt32(base.Request["pagenum"]);
                }
                else
                {
                    this.SetError("友情提示：参数非法");
                }
            }
            if (!this.Page.IsPostBack && (base.Request["id"] != null))
            {
                base.SetSuccess("修改成功！");
                this.Updateorder(Convert.ToInt32(base.Request["id"].ToString()));
            }
            this.getType();
            this.getWeifu();
        }

        public void Updateorder(int id)
        {
            this.ordermodel = this.orderbll.GetModel(id);
            this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
            this.usermodel = this.userbll.GetModel(this.ordermodel.User_id);
            if (this.teammodel.Delivery == "coupon")
            {
                for (int i = 0; i < this.ordermodel.Quantity; i++)
                {
                    this.coupmodel.Team_id = this.ordermodel.Team_id;
                    this.coupmodel.Order_id = id;
                    this.coupmodel.Type = "consume";
                    this.coupmodel.Credit = this.teammodel.Credit;
                    this.coupmodel.Consume = "N";
                    this.coupmodel.User_id = this.ordermodel.User_id;
                    this.coupmodel.Create_time = DateTime.Now;
                    this.coupmodel.Expire_time = this.teammodel.Expire_time;
                    this.coupmodel.Partner_id = this.teammodel.Partner_id;
                    this.coupmodel.Id = Helper.GetRandomString(12);
                    this.coupmodel.Secret = Utility.GetCouponpass();
                    this.coupbll.Add(this.coupmodel);
                    this.sysmodel = this.sysbll.GetModelByCache(1);
                    string sitename = "";
                    if (this.sysmodel != null)
                    {
                        sitename = this.sysmodel.sitename;
                    }
                    List<string> mobiles = new List<string>();
                    mobiles.Add(this.usermodel.Mobile);
                    string content = sitename + "项目：" + this.teammodel.Product + "，编号：" + this.coupmodel.Id + "，密码：" + this.coupmodel.Secret + "，有效期至：" + this.coupmodel.Expire_time.ToString("yyyy年MM月dd日");
                    Maticsoft.BLL.Order.SendSMS(mobiles, content);
                }
            }
            this.ordermodel.State = "pay";
            this.ordermodel.Admin_id = base.AsUser.Id;
            this.ordermodel.Pay_time = new DateTime?(DateTime.Now);
            this.orderbll.Update(this.ordermodel);
            this.getType();
            this.getWeifu();
        }
    }


